home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 9588 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  3.1 KB

  1. Path: lrz-muenchen.de!news
  2. From: watzka@stat.uni-muenchen.de (Kurt Watzka)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: passing arrays and returning structs
  5. Date: 11 Mar 1996 21:54:57 GMT
  6. Organization: Leibniz-Rechenzentrum, Muenchen (Germany)
  7. Distribution: world
  8. Message-ID: <4i27fh$bbd@sparcserver.lrz-muenchen.de>
  9. References: <4i2128$69d@news2.acs.oakland.edu>
  10. NNTP-Posting-Host: sun2.lrz-muenchen.de
  11.  
  12. jggoslin@vela.acs.oakland.edu (Monument) writes:
  13.  
  14. >I got a mess of errors, included below for your perusal.  If anyone
  15. >has any suggestions on how I could get the pointers and returns fixed,
  16. >I would sincerely appreciate it.  Post or email, since I read both
  17. >regularly.
  18.  
  19. >===client.h===
  20. >#include <stdio.h>
  21.  
  22. [some irrelevant headers and definitions edited]
  23.  
  24. >typedef struct my_msgbuf {
  25. >    long mtype;
  26. >    int pid;
  27. >    int size;
  28. >    int data[2*MAXSZ];
  29. >} Message;
  30. >===client.h===
  31.  
  32. >===client.c===
  33. >// header files
  34. >#include "client.h"
  35.  
  36. >// function prototypes
  37. >struct Message transitive_closure(int matrix[]);
  38.  
  39. "struct Message" is an incomplete type. This seems to be the problem
  40. of the day. You have declared a type "struct my_msgbuf", and 
  41. "Message" is an other name for it. "struct Message" is completely
  42. unrelated to both of them, but since it lives in a different 
  43. namespace than "Message" - it is a struct "tag" - it may have the
  44. same name.
  45.  
  46. >// MAINLINE
  47.  
  48. This is a syntax error in C. In a similar language that allows this
  49. kind of comments, both "struct my_msgbuf" and "my_msgbuf" are 
  50. known types, but "struct Message" is still an incomplete type.
  51.  
  52. >int main(int argc, char *argv[])
  53. >{
  54.  
  55. >[here is the relevant call]
  56. >    // get the transitive closure
  57. >    buffer = transitive_closure(matrix);
  58.  
  59. [rest of main() edited]
  60.  
  61. >struct Message* transitive_closure(int matrix[])
  62.  
  63. Is there a good reason to provide a declaration with a different
  64. return type? Your compiler tells you what your problem is in
  65. _very_ clear words:
  66.  
  67. >{
  68. >    int i, j, size;
  69. >    Message buffer;
  70. >    pid_t pid;
  71.  
  72. >[here do I refer to the subtypes in the correct manner?  "." vs. "->"
  73. >is what I'm talking about]
  74.  
  75. >    buffer.mtype=1L;
  76.  
  77. "buffer" is _not_ a pointer, so you can write either
  78.  
  79.    (&buffer)->mtype = 1L;
  80.  
  81. or 
  82.   
  83.    buffer.mytype = 1L
  84.  
  85. Compilers tend to tell you if you use the wrong operator, and they are
  86. required to do so.
  87.  
  88. >    buffer.pid=pid;
  89. >    buffer.size=size;
  90. >    for (i=0; i<size; i++)
  91. >        for (j=0; j<size; j++)
  92. >            buffer.data[i*size+j]=matrix[i*size+j];
  93.  
  94. >[is the return type correct?]
  95. >    return *buffer;
  96.  
  97. Obviously not. Your compiler say that you are trying to dereference
  98. a non-pointer. You want to return a pointer to a local variable.
  99. This cannot be done if the variable has storage class "auto". The
  100. syntax to return a pointer to a local variable with storage class
  101. "static" is "return &buffer;".
  102.  
  103. How about sticking to your original declaration of your function
  104. and leaving the dirty work of figuring out how to pass a "struct
  105. my_msgbuf" to your compiler? in this case, "return buffer;" will
  106. do.
  107.  
  108. >}
  109. >===client.c===
  110.  
  111. Your compiler seems to be quite verbose in its diagnostic messages.
  112. Learn to trust it.
  113.  
  114. Kurt
  115. -- 
  116. | Kurt Watzka                             Phone : +49-89-2180-6254
  117. | watzka@stat.uni-muenchen.de
  118. | ua302aa@sunmail.lrz-muenchen.de
  119.